#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/10/24 23:04
# @Author  : summer
# @Email   : 610658552@qq.com
# @File    : email.py
# @Software: PyCharm
# @Desc    :

from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework import status

from semail.utils.utils_email import EmailSender


class SendEmailCheck(APIView):
    """发送邮件API"""
    def post(self, request):
        try:
            EmailSender.send_email(
                to="610658552@qq.com",
                subject="主题: 测试邮件",
                contents=[
                    "<p>这是一封<strong>测试邮件正文</strong></p>",  # 支持HTML
                ],
                # attachments=data.get('attachments'),  # 如果需要附件
                config_id=None
            )
            return Response({"status": "success", "message": "测试成功"}, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({"status": "error", "message": "测试失败"})


class SendEmailPush(GenericAPIView):
    def post(self, request):
        try:
            data = request.data
            EmailSender.send_email(
                to=data['to'],
                subject=data['subject'],
                contents=data['contents'],
                config_id=data.get('config_id')  # 自动处理None情况
            )
            return Response({"status": "success"})

        except ValueError as e:
            return Response(
                {"status": "error", "message": str(e)},
                status=status.HTTP_400_BAD_REQUEST
            )
        except Exception as e:
            return Response(
                {"status": "error", "message": f"发送失败: {str(e)}"},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )